package cn.stylefeng.guns.modular.controller;

import java.io.File;
import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjDictionary;
import cn.stylefeng.guns.modular.service.IIdjDictionaryService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * <p>
 * 轮播图 前端控制器
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-07
 */
@Controller
@RequestMapping("/modular/dictionary")
public class IdjDictionaryController {

	private static String PREFIX = "/dictionary/";

	private static final int SHORT_MAX = 65536;

	private static int counter = -1;

	@Autowired
	private IIdjDictionaryService service;

	/**
	 * 跳转到用户的首页
	 */
	@RequestMapping
	public String index(ModelMap map) {
		map.put("key", service.selectDictionaryName());
		return PREFIX + "dictionary.html";
	}

	@RequestMapping("details/{id}")
	public ModelAndView detail(@PathVariable Long id) {
		ModelAndView index = service.edit(id);
		index.setViewName(PREFIX + "dictionary_details.html");
		return index;
	}
	
	@RequestMapping("edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView index = service.edit(id);
		index.setViewName(PREFIX + "dictionary_edit.html");
		return index;
	}

	@RequestMapping("/add")
	public String add() {
		return PREFIX + "dictionary_add.html";
	}

	/**
	 * 查询注册用户列表
	 */
	@ResponseBody
	@GetMapping("list")
	public LayuiPageInfo list(String dictionaryName) {
		// 获取分页参数
		Page<IdjDictionary> page = LayuiPageFactory.defaultPage();
		return service.getList(page, dictionaryName);
	}

	@RequestMapping("/update")
	@ResponseBody
	public ResponseData update(@RequestBody IdjDictionary dict) {
		if (ToolUtil.isEmpty(dict.getId())) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		service.update(dict);
		return ResponseData.success();
	}

	@RequestMapping("/insert")
	@ResponseBody
	public ResponseData insert(@RequestBody IdjDictionary dict) {
		dict.setId(nextId());
		dict.setCreateTime(new Date());
		service.insert(dict);
		return ResponseData.success();
	}

	@RequestMapping("/delete/{bannerId}")
	@ResponseBody
	public ResponseData delete(@PathVariable Long bannerId) {
		if (ToolUtil.isEmpty(bannerId)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		service.delete(bannerId);
		return ResponseData.success();
	}
	
	public static synchronized long nextId() {
		long now = System.currentTimeMillis();
		if (counter == -1) {
			long seed = now ^ Thread.currentThread().getId();
			Random rnd = new Random(Long.hashCode(seed));
			counter = rnd.nextInt(SHORT_MAX);
		}
		long id = (now << 16) | counter;
		counter = (counter + 1) % SHORT_MAX;
		return id;
	}
	
	 /**  
     * 删除  
     *   
     * @param files  
     */  
    private void deleteFile(File... files) {  
        for (File file : files) {  
            if (file.exists()) {  
                file.delete();  
            }  
        }  
    }
}
